float4x4 ViewProjection : ViewProjection;
float4 lightPosition0 : LightPosition0;
float4 lightDiffuse0 : LightDiffuse0;

float4 MaterialDiffuse: Diffuse;

float Thickness: FloatParameter0 = 1;


/******************************************************************************/
/*                                                                            */
/*  *********  *******  ****  ****  *********  ****  ****  *******   *******  */
/*  *********  ***       ***  ***   *********  ****  ****  ***  ***  ***      */
/*     ***     *******    **  **       ***     ****  ****  ***  ***  *******  */
/*     ***     *******     ****        ***     ****  ****  *** ***   *******  */
/*     ***     ***        ******       ***      ***  ***   *******   ***      */
/*     ***     *******  ****  ****     ***       ******    ***  ***  *******  */
/*                                                                            */
/******************************************************************************/
texture BaseTexture: TEXTURE0;

sampler2D BaseSampler = sampler_state
{
    Texture = (BaseTexture);
    MIPFILTER = LINEAR;
    MAGFILTER = LINEAR;
    MINFILTER = LINEAR;
	ADDRESSU = CLAMP;
	ADDRESSV = CLAMP;
};

struct VS_INPUT{
	float3 Position: POSITION;
	float4 UV: TEXCOORD0;
	float4 _M1 : TEXCOORD1;
	float4 _M2 : TEXCOORD2;
	float4 _M3 : TEXCOORD3;
	float4 _M4 : TEXCOORD4;
    float3 normal	  : NORMAL;
};


struct VS_OUTPUT{
    float4 pos:POSITION;
    float2 UV: TEXCOORD0;
};

struct vout_outline
{
    float4 position	   : POSITION;
};
 
 

/******************************************************************************/
/*                                                                            */
/*   ***       ***  ********  ********   *********  ********  ***     ***     */
/*    ***     ***   ***       ***   ***  *********  ***        ***   ***      */
/*     ***   ***    ***       ***   ***     ***     ***         *******       */
/*      *** ***     ********  ***   ***     ***     ********     *****        */
/*       *****      ***       ********      ***     ***         *******       */
/*        ***       ***       ***  **       ***     ***        ***   ***      */
/*         *        ********  ***   ***     ***     ********  ***     ***     */
/*                                                                            */
/******************************************************************************/


//VERTEX SHADER
void vs( in VS_INPUT IN, out VS_OUTPUT OUT ) 
{
    float4x4 World =
	{
    IN._M1,
    IN._M2,
    IN._M3,
    IN._M4
	};
	
    float4 worldPos =mul(float4(IN.Position, 1.0), World);
	OUT.pos = mul(float4(worldPos.xyz,1),ViewProjection);
    
	float3 normal   = mul( float4( IN.normal , 0) , World );
	
	float3 lightDir0 = normalize(lightPosition0.xyz - worldPos.xyz);
	
	//use the light direction & normal to determine the intensity
	OUT.UV.xy = float2(1-saturate(dot(lightDir0,normal)),0);
	
	
}

void vs_outline( in VS_INPUT IN, out vout_outline OUT )
{
	float4x4 World =
	{
    IN._M1,
    IN._M2,
    IN._M3,
    IN._M4
	};
	
	float3 normal   = mul( IN.normal , World )*Thickness;
	float3 worldPos =mul(float4(IN.Position, 1.0), World)+normal;
	OUT.position = mul(float4(worldPos,1.0),ViewProjection);
}

 
/******************************************************************************/
/*                                                                            */
/*            ********  ***   ****     ****    ******    ***                  */
/*            **    **  ***    ****   ****     ******    ***                  */
/*            **    **  ***      *** ***       ***       ***                  */
/*            ******    ***       *****        ******    ***                  */
/*            **        ***      *** ***       ***       ***                  */
/*            **        ***     ****  ****     ******    *********            */
/*            **        ***    ****    ****    ******    *********            */
/*                                                                            */
/******************************************************************************/

//PIXEL SHADER
float4 ps( in VS_OUTPUT IN ) : COLOR
{
	float4 lightColor0 = tex2D(BaseSampler, IN.UV);
	return MaterialDiffuse*lightColor0;	
	
}

float4 ps_black (in vout_outline IN ) : COLOR
{
	return float4(0,0,0,1);
}


 
technique ToonEffect
{
    pass p0
	{
		CullMode = None;
		ZEnable=true;
		ZWriteEnable=false;
		AlphaBlendEnable = false;
		vertexshader = compile vs_3_0 vs_outline();
        pixelshader  = compile ps_3_0 ps_black();
	}
    pass p1
    {
        CullMode = CCW;
		ZEnable=true;
		ZWriteEnable=true;
		vertexshader = compile vs_3_0 vs();
        pixelshader  = compile ps_3_0 ps();
    }
	
	
}